<template>
  <div class="MemberInfo-div">
    <el-card>
      <!-- 2.1 顶部操作 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <!-- 使用clearable属性即可得到一个可清空的输入框   clear	在点击由 clearable 属性生成的清空按钮时触发-->
          <el-input placeholder="请输入内容" v-model="queryInfo.type" class="input-with-select" clearable
                    @clear="getMemberInfo">
            <template #append>
              <el-button icon="el-icon-search" @click="search"></el-button>
            </template>
          </el-input>
        </el-col>
        <el-col :span="8">
          <el-button type="primary" @click="showAddDialog">添加</el-button>
          <el-button type="primary" @click="deleteWithArr(multipleSelection)">批量删除</el-button>
        </el-col>
      </el-row>

      <el-table :data="data_show" border stripe
                @selection-change="handleSelectionChange"
      >
        <!-- 索引列 -->
        <el-table-column type="selection" width="60"></el-table-column>
        <el-table-column label="ID" prop="id" width="60" v-if="false"></el-table-column>
        <el-table-column label="ID(编号)" prop="memberId" width="60"></el-table-column>
        <el-table-column label="姓名" prop="memberName"></el-table-column>
        <el-table-column label="性别" prop="memberSex">
          <template slot-scope="row">{{ row.memberSex == 0 ? '男' : '女' }}</template>
        </el-table-column>
        <el-table-column label="电话" prop="memberTel"></el-table-column>
        <el-table-column label="地址" prop="memberAddress"></el-table-column>
        <el-table-column label="邮箱" prop="memberEmail"></el-table-column>
        <el-table-column label="积分" prop="memberIntegral"></el-table-column>
        <el-table-column label="级别" prop="memberLevel"></el-table-column>
        <el-table-column label="最后一次入住时间" prop="checkinTime"></el-table-column>
        <el-table-column label="最后一次预定时间" prop="reserveTime"></el-table-column>
        <el-table-column label="备注" prop="remarks"></el-table-column>
        <el-table-column label="创建时间">
          <template slot-scope="{row}">
            <span>{{ row.createdTime | dateFormat }}</span>
          </template>
        </el-table-column>
        <el-table-column label="修改时间" prop="modifiedTime">
          <template slot-scope="{row}">
            <span>{{ row.modifiedTime | dateFormat }}</span>
          </template>
        </el-table-column>
        <!-- 自定义列 -->
        <el-table-column label="操作" width="180px" fixed="right">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit" @click="showEditDialog(scope.row.id)">编辑
            </el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete" @click="removeById(scope.row.id)">删除
            </el-button>
            <!-- {{scope.row}} -->
          </template>
        </el-table-column>
      </el-table>

      <!-- 2.3 分页 -->

      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page="queryInfo.pageNum" :page-size="queryInfo.pageSize" :page-sizes="[5, 10, 20, 40]"
                     layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>

    <!-- 编辑框 modify  -->
    <el-dialog title="编辑" :visible.sync="editDialogVisible" width="30%" @close="editDialogClosed">
      <el-form :model="editForm" ref="editFormRef" :rules="editFormRules" label-width="80px">
        <el-form-item label="ID" prop="id" hidden>
          <el-input v-model="editForm.id" disabled hidden></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="memberName">
          <el-input v-model="editForm.memberName"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="memberSex">
          <el-radio-group v-model="radio">
            <el-radio :label=1 change>男</el-radio>
            <el-radio :label=0 change>女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="电话" prop="memberTel">
          <el-input v-model.number="editForm.memberTel"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="memberAddress">
          <el-input v-model="editForm.memberAddress"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="memberEmail">
          <el-input v-model="editForm.memberEmail"></el-input>
        </el-form-item>
        <el-form-item label="级别" prop="memberLevel">
          <el-input v-model.number="editForm.memberLevel"></el-input>
        </el-form-item>
        <el-form-item label="积分" prop="memberIntegral">
          <el-input v-model.number="editForm.memberIntegral"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input type="textarea" v-model="editForm.remarks"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click.native="updateMemberInfo">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 添加框 add  -->
    <el-dialog title="添加" :visible.sync="addDialogVisible" width="30%" @close="addDialogClosed">
      <el-form :model="addForm" ref="editFormRef" :rules="editFormRules" label-width="80px">
        <el-form-item label="ID" prop="id" hidden>
          <el-input v-model="addForm.id" disabled hidden></el-input>
        </el-form-item>
        <el-form-item label="会员编号" prop="memberId">
          <el-input v-model="addForm.memberId"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="memberName">
          <el-input v-model="addForm.memberName"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="memberPwd">
          <el-input v-model="addForm.memberPwd"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="memberSex">
          <el-radio-group v-model="addForm.memberSex">
            <el-radio :label=1 change>男</el-radio>
            <el-radio :label=0 change>女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="电话" prop="memberTel">
          <el-input v-model.number="addForm.memberTel"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="memberAddress">
          <el-input v-model="addForm.memberAddress"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="memberEmail">
          <el-input v-model="addForm.memberEmail"></el-input>
        </el-form-item>
        <el-form-item label="级别" prop="memberLevel">
          <el-input v-model.number="addForm.memberLevel"></el-input>
        </el-form-item>
        <el-form-item label="积分" prop="memberIntegral">
          <el-input v-model.number="addForm.memberIntegral"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input type="textarea" v-model="addForm.remarks"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click.native="addMemberInfo">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getMemberInfoList,
  deleteMemberInfo,
  deleteOne,
  getMemberINfoById,
  updateMemberInfo,
  addMemberInfo
} from "../../api/member_info_manage";

export default {

  name: "MemberInfoManage",
  data() {
    return {
      addForm: {
        id: '',
        memberId: '',
        memberSex: null,
        memberPwd: '',
        memberTel: null,
        memberAddress: '',
        memberEmail: '',
        memberIntegral: '',
        memberLevel: '',
        remarks: ''
      },
      queryInfo: {
        type: "",
        pageNum: 1,
        pageSize: 10,
      },
      data_show: [],

      multipleSelection: [],
      ids: [],

      editForm: {},
      editDialogVisible: false,
      addDialogVisible: false,
      editFormRules: {
        memberId: [
          {required: true, message: "类型不能为空", trigger: "blur"},
          {min: 1, max: 10, message: "长度在 1 到 10 个字符", trigger: "blur"}
        ],
        memberName: [
          {required: true, message: "类型不能为空", trigger: "blur"},
          {min: 1, max: 10, message: "长度在 1 到 5 个字符", trigger: "blur"}
        ],
        memberPwd: [
          {required: true, message: "类型不能为空", trigger: "blur"},
          {min: 1, max: 18, message: "长度在 1 到 18 个字符", trigger: "blur"}
        ],
        memberSex: [
          {required: true, message: "性别不能为空", trigger: "blur"}
        ],
        memberTel: [
          {required: true, message: '请输入手机号', trigger: 'blur'},
          {type: 'number', message: '手机号必须为数字'},
          {
            pattern: /^0{0,1}(13[0-9]|15[7-9]|153|156|18[7-9])[0-9]{8}$/,
            message: '手机号格式不对',
            trigger: 'blur'
          }
        ],
        memberAddress: [
          {required: true, message: '请输入地址', trigger: 'blur'},
        ],
        memberEmail: [
          {required: true, message: '请输入邮箱', trigger: 'blur'},
          {
            pattern: /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/,
            message: '邮箱格式不对',
            trigger: 'blur'
          }
        ],
        memberLevel: [
          {required: true, message: '请输入摘要', trigger: 'blur'},
          {type: 'number', message: '级别必须为数字'}
        ],
        memberIntegral: [
          {required: true, message: '请输入积分', trigger: 'blur'},
          {type: 'number', message: '积分必须为数字'}
        ],
        remarks: [
          {required: true, message: '请输入摘要', trigger: 'blur'}
        ],
      },
      radio: 1
    }
  },
  created() {
    this.getMemberInfo();
  },
  methods: {
    // 添加会员
    addMemberInfo() {
      addMemberInfo(this.addForm).then(res => {
        if (res.status !== 200) {
          this.$notify.error({
            title: '错误',
            message: res.msg
          });
          this.addDialogVisible = false;
          return
        }
        this.$notify({
          title: '成功',
          message: res.msg,
          type: 'success'
        });
        this.getMemberInfo();
      }).catch(res => {
        this.$notify.error({
          title: '错误',
          message: res.msg
        });
      });
      this.addDialogVisible = false;
    },
    // 查询全部
    getMemberInfo() {
      getMemberInfoList(this.queryInfo.pageNum, this.queryInfo.pageSize, null).then((res) => {
        // console.log(res.data.records);
        this.data_show = res.data.records;
      })
    },
    // 展示修改对象
    showEditDialog(id) {
      getMemberINfoById(id).then((res) => {
        if (res.status !== 200) {
          this.$notify.error({
            title: '错误',
            message: res.msg
          });
        }
        console.log(res.data)
        this.editForm = res.data // 对象复制
        this.radio = this.editForm.memberSex;
        this.editDialogVisible = true
      })
    },
    // 修改会员信息
    updateMemberInfo() {
      this.editForm.memberSex = this.radio;
      // console.log(this.editForm);
      updateMemberInfo(this.editForm).then(res => {
        if (res.status === 200) {
          this.$notify({
            title: '成功',
            message: res.msg,
            type: 'success',
            duration: 1200
          })
        } else {
          return this.$message.error("删除失败")
        }
        this.getMemberInfo();
      }).catch((res) => {
        this.$notify.error({
          title: '错误',
          message: res.msg
        });
      })
      this.editDialogVisible = false;
    },
    // 展示添加框
    showAddDialog() {
      this.addDialogVisible = true;
    },
    // 删除
    removeById(id) {
      this.$confirm('此操作将永久删除数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // this.ids.push(id)
        // 删除逻辑
        deleteOne(id).then(res => {
          if (res.status === 200) {
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 1200
            })
          } else {
            return this.$message.error("删除失败")
          }
          this.getMemberInfo();
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    deleteWithArr(idArr) {
      console.log(idArr);
      idArr.forEach(element => {
        this.ids.push(element.id)
      });
      console.log("ids：" + this.ids)

      deleteMemberInfo(this.ids).then(res => {
        if (res.status === 200) {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 1200
          })
        } else {
          return this.$message.error("删除失败！")
        }
        this.getMemberInfo();
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      console.log(this.multipleSelection)
    },

    //分页
    handleSizeChange(val) {
      //console.log(`每页 ${val} 条`);
      this.queryInfo.pageSize = val;
      // this.getList();
    },
    handleCurrentChange(val) {
      //console.log(`当前页: ${val}`);
      this.queryInfo.pageNum = val; // 获取新页码
      // this.getList();
    },

    // 搜索
    search() {
      getMemberInfoList(this.queryInfo.pageNum, this.queryInfo.pageSize, this.queryInfo.type).then((res) => {
        // console.log(res.data.records);
        this.data_show = res.data.records;
      })
    },

    // 面板关闭
    addDialogClosed(){
      this.addDialogVisible = false;
    },
    editDialogClosed(){
      this.editDialogVisible = false;
    }
  }
}
</script>

<style scoped>

</style>